﻿//=============================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 作 者：江怀玉
// 日 期：2011/11/17
// 描 述: 通用的查询数据功能[YUI]
// URL Parameter：
//        entity:对应的实体
//        condition:过滤数据条件
//        order:排序字段
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00   2011/11/17   江怀玉           
//=============================================================
using System;
using System.Linq;
using ZX.DBModule;
using ZX.BLL.Extend;
using System.Collections.Generic;
using System.Collections;


namespace ZX.Web.UI.Controls
{
    public partial class YUILayout :ListPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public string FieldInfo
        {
            get
            {
                return setFieldInfo();
            }
        }

        public string Table
        {
            get
            {
                return GetStringFromQueryString("table", "");
            }
        }

        public Guid TableID
        {
            get
            {
                return GetGuidFromQueryString("tableid");
            }
        }

        private string setFieldInfo()
        {
            //string strEntity = this.GetStringFromQueryString("entity", "E_Menu");
            BaseEntity instance = ZX.Common.Globals.getEntity("DataListTemplate") as BaseEntity;
            BaseBussnies bInstance = new BLL.Extend.BaseBussnies(instance);
            //bInstance.Order = "iOrder";
            UserListTemplate entity = bInstance.GetEntityByCondition<UserListTemplate>("EntityName='" + Table + "' and AddUser='"+UserId+"'");
            IList<DataListTemplate> list= null;
            if (entity != null)
            {
                list = (List<DataListTemplate>)ZX.Common.Globals.JSonToObject<List<DataListTemplate>>(entity.DataJSON).OrderBy(p=>p.iOrder).ToList();
            }
            else
            {
                list = bInstance.GetEntitys<DataListTemplate>("EntityName='" + Table + "'");
            }
            
            if (list == null)
            {
                return "[]";
            }
            return ZX.Common.Globals.ObjectToJSON(list);
        }


        public string Instance
        {
            get
            {
                return ZX.Common.Globals.ObjectToJSON(new DataListTemplate());
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public string strJson
        {
            get
            {
                //if (IndexOf("QueryForm") > -1)
                {
                    ZX.DBModule.QuerySolution entity = new QuerySolution();
                    return ZX.Common.Globals.ObjectToJSON(entity);
                }
                ////else
                //{
                //    return "";
                //}
            }
        }

        /// <summary>
        /// 字段列表
        /// </summary>
        public string FieldList
        {
            get
            {
                return setFieldInfo();
                //BaseBussnies bll = new BaseBussnies();
                //IList<DataListTemplate> list = bll.GetEntitys<DataListTemplate>(" EntityName ='" + this.Table + "'").OrderBy(p => p.iOrder).ToList();
                //List<SysTableField> TFlist = new List<SysTableField>();
                //foreach (var entity in list)
                //{
                //    SysTableField stf = new SysTableField();
                //    stf.TableFieldID = Guid.NewGuid();
                //    stf.TableID = Guid.NewGuid();
                //    stf.TableName = entity.TableName;
                //    stf.Is_PrimaryKey = entity.PKey == null ? "N" : entity.PKey.Trim();
                //    stf.Is_Null = entity.CanNull == null ? "Y" : entity.CanNull.Trim();
                //    stf.FieldName = entity.ColName;
                //    stf.FiledType = entity.CType;
                //    stf.Notes = entity.ColDesc;
                //    stf.Length = entity.Length.ToString();
                //    TFlist.Add(stf);
                //}
                //return ZX.Common.Globals.ObjectToJSON(list);
            }
        }

        /// <summary>
        /// 设置默认查询
        /// </summary>
        public string DefineCondition
        {
            get;
            set;
        }

        /// <summary>
        /// 解决方案
        /// </summary>
        public string Solution
        {
            get;
            set;
        }


        //public string SolutionList
        //{
        //    get;
        //    set;
        //}
        //private void setSolution()
        //{
        //    ZX.BLL.Extend.BaseBussnies bll = new BLL.Extend.BaseBussnies();
        //    IList<QuerySolution> list = bll.GetEntitys<QuerySolution>("AddUser='"+this.UserId+"' and FunctionID='" + this.FunctionID + "'");
        //    if (list != null)
        //    {

        //        QuerySolution entity = list.Where(p => p.IsDefault == "Y").FirstOrDefault();
        //        DefineCondition = entity == null ? "" : entity.Condition;
        //        StringBuilder select = new StringBuilder("<select id='HistorySolution' onchange='ChangeSolution(this);'>");
        //         select.Append("<option value=\""+(new Guid())+"\"></option>");
        //        foreach (var o in list)
        //        {
        //            select.Append(string.Format("<option value=\"{0}\">{1}</option>", o.QueryId, o.SolutionName));
        //        }
        //        select.Append("</select>");
        //        Solution = select.ToString();
        //        SolutionList = ZX.Common.Globals.ObjectToJSON(list);
        //    }
        //    else
        //    {
        //        StringBuilder select = new StringBuilder("<select id='HistorySolution'>");
        //        select.Append("<option value=\"" + (new Guid()) + "\"></option>");
        //        select.Append("</select>");
        //        Solution = select.ToString();
        //        SolutionList = "[]";
        //    }
        //}
    }
}